package pkg

import (
	"fmt"
	openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
	dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v4/client"
	console "github.com/alibabacloud-go/tea-console/client"
	util "github.com/alibabacloud-go/tea-utils/v2/service"
	"github.com/alibabacloud-go/tea/tea"
	"github.com/smartwalle/alipay/v3"
	"server/basic/config"
)

// Description:
//
// 使用AK&SK初始化账号Client
//
// @return Client
//
// @throws Exception
func CreateClient() (_result *dysmsapi20170525.Client, _err error) {
	// 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考。
	// 建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378661.html。
	config := &openapi.Config{
		// 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
		AccessKeyId: tea.String(config.Conf.ALiYun.AccessKeyId),
		// 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
		AccessKeySecret: tea.String(config.Conf.ALiYun.AccessKeySecret),
	}
	// Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
	config.Endpoint = tea.String("dysmsapi.aliyuncs.com")
	_result = &dysmsapi20170525.Client{}
	_result, _err = dysmsapi20170525.NewClient(config)
	return _result, _err
}

func ALiYun(phone, code string) (resp *dysmsapi20170525.SendSmsResponse, _err error) {
	client, _err := CreateClient()
	if _err != nil {
		return resp, _err
	}

	sendSmsRequest := &dysmsapi20170525.SendSmsRequest{
		SignName:      tea.String("阿里云短信测试"),
		TemplateCode:  tea.String("SMS_154950909"),
		PhoneNumbers:  tea.String(phone),
		TemplateParam: tea.String("{\"code\":\"" + code + "\"}"),
	}
	runtime := &util.RuntimeOptions{}

	resp, _err = client.SendSmsWithOptions(sendSmsRequest, runtime)
	if _err != nil {
		return resp, _err
	}

	console.Log(util.ToJSONString(resp))

	return resp, _err

}

func Alipay(money string) {
	var privateKey = "MIIEvQIBADANB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DL9hMweC9RNMVykCU63Nh4wVFHlOwegQv+cd0atFs+zweYbRiMNwW7o2zi1gmFs6LCp/TcTaBszVD8/7lpq7FRrWI5s/UOaB+qds3IGTt18PCEL8RzvsyU/HBz5YwiqNEBjsu7UU/YG0/XLtvwnBarDsunM83WeRypucUKsDRMWwKBgC9sfEQ0vSuqLsc8znmxdhZkJkEHAVnR8V1sn2gPvKIIK/fM1jpXDuqpUEzbfmIYMg39G6dESbGumwkCpSIxt/TWd+gMSrPqpmagM8tk8jbcAdeJZP68upZenL0xC0TA4illl91Nqwlyl4S1B+hLLelHr6Us7vqHFXS2yc+nCRBtAoGAMHRf3CrPtSBZRTq5XAlbrdECsjn/03giKkYeXldW/6ARXhXnGYLOLFCfUChQo1Dp2OzyS57SO7iQPFJMStdhxKRktDLLFkNcm7KIj3NaAyE68qnaygCfS9Y7Jjpjg9RSvCvS6yJyelO+IoLtwtKXOuihcM9bKI3kTBpDtSZ+vNM=" // 必须，上一步中使用 RSA签名验签工具 生成的私钥
	client, err := alipay.New("9021000142691074", privateKey, false)
	if err != nil {
		fmt.Println(err)
		return
	}

	var p = alipay.TradeWapPay{}
	p.NotifyURL = "http://xxx"
	p.ReturnURL = "http://baidu.com"
	p.Subject = "标题"
	p.OutTradeNo = "传递一个唯一单号"
	p.TotalAmount = money
	p.ProductCode = "QUICK_WAP_WAY"

	url, err := client.TradeWapPay(p)
	if err != nil {
		fmt.Println(err)
	}

	// 这个 payURL 即是用于打开支付宝支付页面的 URL，可将输出的内容复制，到浏览器中访问该 URL 即可打开支付页面。
	var payURL = url.String()
	fmt.Println(payURL)
}
